//
//  TodaysPlanMemoViewController.m
//  TodaysPlan
//
//  Created by 寺田 篤史 on 11/10/13.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "TodaysPlanMemoViewController.h"
#import "ConfigManager.h"

@implementation TodaysPlanMemoViewController

@synthesize memoTextView,memoString;
static BOOL _registered;

- (void)loadView {
    CGRect bounds = [[UIScreen mainScreen] applicationFrame];
    [super loadView];
    memoTextView = [[UITextView alloc] initWithFrame:bounds];
    //memoTextView.text = todaysplan.memo;
    ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
    memoTextView.text = [config.cfgMemoData getValueString];
    [self.view addSubview:memoTextView];
}

- (void)viewWillAppear:(BOOL)animated
{
    // super
    [super viewWillAppear:animated];
    
    // Register for notifiactions
    if (!_registered) {
        NSNotificationCenter *center;
        center = [NSNotificationCenter defaultCenter];
        [center
         addObserver:self
         selector:@selector(keyboardWillShow:)
         name:UIKeyboardWillShowNotification
         object:nil];
        [center
         addObserver:self
         selector:@selector(keybaordWillHide:)
         name:UIKeyboardWillHideNotification
         object:nil];
        
        _registered = YES;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    // super
    [super viewWillDisappear:animated];
    
    // Unregister from notification center
    if (_registered) {
        NSNotificationCenter *center;
        center = [NSNotificationCenter defaultCenter];
        [center
         removeObserver:self
         name:UIKeyboardWillShowNotification
         object:nil];
        [center
         removeObserver:self
         name:UIKeyboardWillHideNotification
         object:nil];
        
        _registered = NO;
    }
}
- (void)keyboardWillShow:(NSNotification*)notification
{
    // Get userInfo
    NSDictionary *userInfo;
    userInfo = [notification userInfo];
    
    // Calc overlap of keyboardFrame and textViewFrame
    CGFloat overlap;
    CGRect keyboardFrame;
    CGRect textViewFrame;
    keyboardFrame = [[userInfo
                      objectForKey:UIKeyboardFrameEndUserInfoKey]
                     CGRectValue];
    keyboardFrame = [memoTextView.superview
                     convertRect:keyboardFrame
                     fromView:nil];
    textViewFrame = memoTextView.frame;
    overlap = MAX(0.0, CGRectGetMaxY(textViewFrame) - CGRectGetMinY(keyboardFrame));
    
    // Calc textViewFrameEnd
    CGRect textViewFrameEnd;
    textViewFrameEnd = memoTextView.frame;
    textViewFrameEnd.size.height -= overlap;
    
    // Animate frame of _textView
    NSTimeInterval duration;
    UIViewAnimationCurve animationCurve;
    void (^animations)(void);
    duration = [[userInfo
                 objectForKey:UIKeyboardAnimationDurationUserInfoKey]
                doubleValue];
    animationCurve = [[userInfo
                       objectForKey:UIKeyboardAnimationCurveUserInfoKey]
                      integerValue];
    animations = ^(void) {
        memoTextView.frame = textViewFrameEnd;
    };
    [UIView
     animateWithDuration:duration
     delay:0.0
     options:(animationCurve << 16)
     animations:animations
     completion:nil];
    
    // Show done item
    UIBarButtonItem *doneItem;
    doneItem = [[[UIBarButtonItem alloc]
                 initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                 target:self
                 action:@selector(doneAction:)]
                autorelease];
    [self.navigationItem
     setRightBarButtonItem:doneItem
     animated:YES];
}
- (BOOL)textView:(UITextView*)textView 
shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString*)text {
    if (text.length>0 &&
        textView.text.length+text.length>140) {
        return NO;
    }
    //todaysplan.memo = [NSString stringWithFormat:@"%@",textView.text];
    return YES;
}
- (void)doneAction:(id)sender
{
    ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
    [config.cfgMemoData setValueString:memoTextView.text];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)keybaordWillHide:(NSNotification*)notification
{
    // Get userInfo
    NSDictionary *userInfo;
    userInfo = [notification userInfo];
    
    // Animate frame and insets of _textView
    NSTimeInterval duration;
    UIViewAnimationCurve animationCurve;
    void (^animations)(void);
    duration = [[userInfo
                 objectForKey:UIKeyboardAnimationDurationUserInfoKey]
                doubleValue];
    animationCurve = [[userInfo
                       objectForKey:UIKeyboardAnimationCurveUserInfoKey]
                      integerValue];
    animations = ^(void) {
        memoTextView.frame = self.view.bounds;
        memoTextView.contentInset = UIEdgeInsetsZero;
        memoTextView.scrollIndicatorInsets = UIEdgeInsetsZero;
    };
    [UIView
     animateWithDuration:duration
     delay:0.0
     options:(animationCurve << 16)
     animations:animations
     completion:nil];
    
    [self.navigationItem setRightBarButtonItem:nil animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
- (void)viewDidUnload
{
    [super viewDidUnload];
}
@end
